﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;
using Sponsorship.Infrastructure;
using Sponsorship.ReadModel;
using Sponsorship.ReadModel.Sponsors;
using Sponsorship.Ui.Wpf.Commanding;
using Sponsorship.Ui.Wpf.Locator;
using Sponsorship.Ui.Wpf.Messaging;

namespace Sponsorship.Ui.Wpf.ViewModel
{
    [Locator ("SponsorListViewModel")]
    public class SponsorListViewModel : ViewModelBase
    {
        private readonly IBus _bus;
        private readonly IReadRepository _readRepository;

        private RelayCommand _backCommand;
        private RelayCommand _editCommand;
        private readonly ObservableCollection<SponsorListItemDto> _sponsors = new ObservableCollection<SponsorListItemDto>();

        public SponsorListViewModel (IBus bus, IReadRepository readRepository)
        {
            _bus = bus;
            _readRepository = readRepository;

            Load();
        }


        private RelayCommand _NewSponsorCommand;
        public ICommand NewSponsorCommand
        {
            get
            {
                return _NewSponsorCommand ?? (_NewSponsorCommand = new RelayCommand(
                                                                       param => NewSponsor(),
                                                                       param => true
                                                                       ));
            }
        }

        private void NewSponsor()
        {
            Messenger.Instance.Send(this, "addsponsor");            
        }

        public ICommand BackCommand
        {
            get
            {
                if (_backCommand == null)
                    _backCommand = new RelayCommand (GoBack);
                return _backCommand;
            }
        }

        public ICommand EditCommand
        {
            get
            {
                return _editCommand ?? (_editCommand = new RelayCommand (
                                                           param => Edit(),
                                                           param => CanEdit
                                                           ));
            }
        }

        private bool CanEdit
        {
            get { return SelectedItem != null; }
        }
      

        public SponsorListItemDto SelectedItem { get; set; }

        public ObservableCollection<SponsorListItemDto> Sponsors
        {
            get { return _sponsors; }
        }

        private void Edit ()
        {
            Messenger.Instance.Send (this, "edit");
        }

        private void GoBack (object param)
        {
            Messenger.Instance.Send (this, "mainmenu");
        }

        private void Load()
        {
            Sponsors.Clear();

            List<SponsorListItemDto> repositoryItems = _readRepository.GetAll<SponsorListItemDto>().AsEnumerable().ToList();
            foreach (var item in repositoryItems)
                Sponsors.Add (item);
        }

        public override void SetModel(Guid id)
        {
            Load();
        }

        
    }
}